import 'dart:async';

import 'package:todo_app/components/Button/CustomElevateButton.dart';
import 'package:todo_app/components/Button/CustomOutlinedButton.dart';
import 'package:todo_app/constants/DColor.dart';
import 'package:todo_app/constants/Screen.dart';
import 'package:flutter/material.dart';

import 'OverlaySupport.dart';

enum OverlayConfirmActionType {
  CANCEL,
  OK,
}

class OverlayConfirmAction {
  OverlayConfirmAction({ this.text, this.type = OverlayConfirmActionType.OK });
  String? text;
  OverlayConfirmActionType type;

  OverlayConfirmAction.cancle({ this.text = "取消", this.type = OverlayConfirmActionType.CANCEL });
  OverlayConfirmAction.ok({ this.text = "确定", this.type = OverlayConfirmActionType.OK });
}

Future<int> showOverlayConfirm({
  String? title,
  String? content,
  Widget? child,
  List<OverlayConfirmAction>? actions,
  Color? maskColor,
  bool showClose = true,
  EdgeInsetsGeometry? contentMargin,
  EdgeInsetsGeometry? contentPadding,
  double? width,
}) {
  assert(content == null || child == null, "不能同时传递child和content");
  actions ??= [
    OverlayConfirmAction(
      text: "确定"
    )
  ];

  var completer = Completer<int>();
  OverlayEntry? overlay;
  var overlayContent = Container(
    decoration: BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(
        Screen.sizeByDesign(15)
      )
    ),
    padding: contentMargin ?? EdgeInsets.only(
      bottom: Screen.sizeByDesign(31),
      left: Screen.sizeByDesign(34),
      right: Screen.sizeByDesign(34),
      top: Screen.sizeByDesign(45),
    ),
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          padding: contentPadding ?? EdgeInsets.symmetric(horizontal: Screen.sizeByDesign(16)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              title != null ?
              Text.rich(
                TextSpan(text: title, style: TextStyle(
                  fontSize: Screen.sizeByDesign(16),
                  height: 24 / 16,
                  color: Color(0xFF464646),
                  fontWeight: FontWeight.bold
                )),
                textAlign: TextAlign.center,
              ) : Container(),
              child != null || content != null ? SizedBox(height: Screen.sizeByDesign(16),) : Container(),
              content != null ? 
                Text.rich(
                  TextSpan(text: content, style: TextStyle(
                    fontSize: Screen.sizeByDesign(16),
                    height: 21 / 16,
                    color: Color(0xFF666666),
                  )),
                  textAlign: TextAlign.center,
                ) :
                Container(),
              child != null ? child : Container(),
            ],
          )
        ),
        SizedBox(height: Screen.sizeByDesign(25)),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: actions.map((e) {
            var index = actions?.indexOf(e);

            if(e.type == OverlayConfirmActionType.OK) {
              return CustomElevateButton(
                onTap: () {
                  overlay?.remove();
                  completer.complete(index);
                },
                child: Text(e.text!),
                height: Screen.sizeByDesign(35),
                width: Screen.sizeByDesign(83),
              );
            } else {
              return CustomOutlineButton(
                borderColor: DColor.descText,
                style: TextStyle(color: DColor.descText,),
                onTap: () {
                  overlay?.remove();
                  completer.complete(index);
                },
                child: Text(e.text!),
                height: Screen.sizeByDesign(35),
                width: Screen.sizeByDesign(83),
              );
            }
          }).toList()
        )
      ],
    )
  );

  overlay = showOverlay(
    builder: (BuildContext _, OverlaySupportState state) {
      return Container(
        alignment: Alignment.center,
        color: maskColor ?? Colors.black.withOpacity(0.6),
        child: Container(
          width: width ?? Screen.sizeByDesign(267),
          child: overlayContent
        )
      );
    }
  );
  return completer.future;
} 